package cn.itcast.controller;


import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController{
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;
    @Autowired
    private ModuleService moduleService;
	@RequestMapping("/login")
	public String login(String email,String password) {
	    //查询指定email的数据是否为空，以及查询出数据
	   //User user=userService.findByEmail(email);
	    if(StringUtils.isBlank(email)||StringUtils.isBlank(password)){
	        request.setAttribute("error","用户名或密码为空");
	        return "forward:/login.jsp";
        }
        password=new Md5Hash(password,email,2).toString();
	    //创建令牌，获取主题，开始认证
        UsernamePasswordToken token = new UsernamePasswordToken(email, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            request.setAttribute("error","用户名或密码错误");
            return "forward:/login.jsp";
        }
        User user1 = (User) subject.getPrincipal();
        session.setAttribute("loginUser",user1);
        List<Module> moduleList = moduleService.findModuleListByUser(user1);
        //System.out.println("模块名："+moduleList);
        session.setAttribute("modules",moduleList);
        return "home/main";





        /*else if(user==null){
	        request.setAttribute("error","用户名不存在");
	        return "forward:/login.jsp";
        }else{
            String s = new Md5Hash(password, user.getEmail(), 2).toString();
            if(!s.equals(user.getPassword())){
	            request.setAttribute("error","密码错误");
	            return "forward:/login.jsp";
            }
        }
        //将登陆后的数据放在session域当中
        session.setAttribute("loginUser",user);
	    //查询该员工具有的权限，具体的判断在service层中
	    List<Module> list=moduleService.findModuleListByUser(user);
	    //将权限放在session域当中
	    session.setAttribute("modules",list);
		return "home/main";*/
	}
    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
	    /*session.removeAttribute("loginUser");*/
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
